<template>
  <div class="note">
    <section class="content" title="笔记">
      <div class="left">
        <NoteItem
          v-for="(item, index) in showList[0]"
          :key="item.id + '' + index"
          :item="item"
        />
      </div>
      <div class="right">
        <NoteItem
          v-for="(item, index) in showList[1]"
          :key="item.id + '' + index"
          :item="item"
        />
      </div>
    </section>
    <div class="loading"><van-loading /></div>
  </div>
</template>

<script>
import NoteItem from "@/components/NoteItem.vue";
export default {
  components: {
    NoteItem,
  },
  data: () => {
    return {
      collectionList: [],
      leftHeight: 0,
      rightHeight: 0,
      showList: [[], []],
      num: 0,
      loading:true
    };
  },
  watch: {
    num(val) {
      if (val >= this.collectionList.length) {
        let cat = [[], []];
        this.collectionList.map((item) => {
          let imgSrc = item.entity.img;
          let img = new Image();
          img.src = imgSrc;
          let width = img.width;
          let scale = width / 750;
          let height = img.height;
          height = Number.parseInt(height) / scale;
          console.log(width, height);
          if (this.leftHeight <= this.rightHeight) {
            cat[0].push({ note: item });
            this.leftHeight += height;
          } else {
            cat[1].push({ note: item });
            this.rightHeight += height;
          }
        });
        console.log(cat);
        this.showList = cat;
        this.loading = false;
      }
    },
  },
  activated() {
    this.leftHeight = 0;
    this.rightHeight = 0;
    this.num = 0;
    this.loading = true;
    this.collectionList = JSON.parse(localStorage.getItem("noteList")) || [];
    this.collectionList.map((item) => {
      let imgSrc = item.entity.img;
      let img = new Image();
      img.src = imgSrc;
      img.onload = () => {
        this.num++;
      };
    });
    console.log(this.collectionList);
  },
};
</script>

<style lang="scss" scoped>
.note {
  width: 100%;
  box-sizing: border-box;
  padding: 0.1rem 0.05rem;
  .content {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
  .loading{
    display: flex;
    justify-content: center;
    padding: 0.5rem 0;
  }
}
</style>